package com.xiaolun;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author 硕鼠
 * @Date 2020/5/7 20:00
 * @Version 1.0
 * @Description 实现多线程同步下载
 */
public class Test02 extends Thread{
    private String url; //网络图片地址
    private String name; //保存的文件名

    public Test02(String url,String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        try {
            webDownloader.downloader(url, name);
            System.out.println("下载的文件名："+ name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Test02 t1 = new Test02("http://pic.ruiwen.com/allimg/1708/598c8d3752d8939511.jpg?x-oss-process=style/qr.ruiwen", "1.jpg");
        Test02 t2 = new Test02("http://pic.ruiwen.com/allimg/1708/598c8d37882aa94153.jpg?x-oss-process=style/qr.ruiwen", "2.jpg");
        Test02 t3 = new Test02("http://pic.ruiwen.com/allimg/1708/598c8d37c8e9a56734.jpg?x-oss-process=style/qr.ruiwen", "3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}

//下载器
class WebDownloader{

    //下载方法
    public void downloader(String url,String name) throws Exception {
        FileUtils.copyURLToFile(new URL(url), new File(name));
    }

}